package com.leador.jni;

import android.content.Context;
import android.util.Log;

import com.leador.location.LocationTrace;
import com.leador.utils.JsonTool;
import com.leador.utils.SharedPreferUtils;

public  class TraceJniInterface {
    /**
     * 清除数据
     * @param context
     */
    public static void clearGPSData(Context context){
        SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
        cacheUtil.putMap("location",null);
    }

    /**
     * 保存坐标数据
     * @param json
     * @param context
     */
    public static void setGPSTrackData(String json, Context context){
        json = json.replace("{\"$change\":","");
        SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
        cacheUtil.putMap("location",json);
    }

    /**
     * 获取数据
     * @param context
     */
    public static byte[] buildLocData(Context context){
        SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
        String json = cacheUtil.getMap("location");
        if(json == null || json.equals("")){
            return null;
        }
        return json.getBytes();
    }

    /**
     * 设置发送包数据
     * @param readySendData
     * @param context
     */
    public static void setSocketPackData(byte[] readySendData,Context context){
        String str = new String(readySendData);
        SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
        cacheUtil.putMap("packData",str);
    }

    /**
     * 获取发送包数据
     * @param context
     * @return
     */
    public static  byte[] buildRequestData(Context context){
            SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
            String json = cacheUtil.getMap("packData");
            return json.getBytes();
    };

    /**
     * 获取心跳包
     * @return
     */
    public  static byte[] buildHeartbeatData(){
        return null;
    };

    /**
     * 自定义
     * @param key
     * @param value
     */
    public static void addCustomData(String key, String value,Context context){
        SharedPreferUtils cacheUtil = SharedPreferUtils.getInstance(context);
        String json = cacheUtil.getMap("location");
        if(json == null || json.equals("")){
            return ;
        }
        LocationTrace locationTrace = JsonTool.toBean(json,LocationTrace.class);
        if(locationTrace != null){
            String custom = locationTrace.getExtend1();
            if(custom == null || custom.equals("")||custom.equals("null")){
                custom =key+"-"+value;
            }else{
                custom += ";"+key+"-"+value;
            }
            locationTrace.setExtend1(custom);
            String jsonStr = JsonTool.toJson(locationTrace);
            String json_result = jsonStr.replace("{\"$change\":","");
//            Log.e("自定义数据",json_result);
            cacheUtil.putMap("location",json_result);
        }
    };
}
